#!/bin/bash
echo "$(date +%F_%T) Check flume Process Starting ..."

DINGTALK_URL="https://oapi.dingtalk.com/robot/send?access_token=1dde01823eae634bd89c2bc043dd968f4f5c4db1bc9300370c2df9f9a136b3b2"
HOSTNAME=$(hostname)

PID=$(ps -ef | grep 'flume.node.Application' |grep 2241| grep -v grep | awk '{print $2}')

if [ -z $PID ];then
    echo "$(date +%F_%T) flume is Not Running ! Start flume ..."
    /bin/sh /usr/local/flume/app2kafka_flume.sh start
    if [ $? == 0 ];then
        echo "$(date +%F_%T) Start flume OK!"
        curl $DINGTALK_URL -H 'Content-Type: application/json' \
         -d '{"msgtype": "markdown","markdown": {"title":"生产环境 flume 发生异常告警","text": "### 生产环境 flume 发生异常\n机器：'$HOSTNAME'\n\n已自动重启成功，请及时确认！"},"at": {"atMobiles": ["18616065561"],"isAtAll": "False"}}'
    else
        echo "$(date +%F_%T) Start flume Failed!"
        curl $DINGTALK_URL -H 'Content-Type: application/json' \
         -d '{"msgtype": "markdown","markdown": {"title":"生产环境 flume 发生异常告警","text": "### 生产环境 flume 发生异常\n机器：'$HOSTNAME'\n\n自动重启失败，请及时排查！"},"at": {"atMobiles": ["18616065561"],"isAtAll": "False"}}'
    fi
else
    echo "$(date +%F_%T) flume (pid:$PID) is Running ..."